package com.njit.office.service.impl;

import com.njit.office.dao.LoginUserMapper;
import com.njit.office.dao.UserMapper;
import com.njit.office.dataobject.LoginUser;
import com.njit.office.dataobject.User;
import com.njit.office.model.Result;
import com.njit.office.service.UserService;
import com.njit.office.utils.IDUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    LoginUserMapper loginUserMapper;

    @Resource
    UserMapper userMapper;

    private String slat = "office";



    @Override
    public LoginUser login(String email, String pwd) {

        LoginUser loginUser = loginUserMapper.selectByEmail(email);

        if (loginUser == null) {
            return new LoginUser();
        }

        String userId = loginUser.getUserId();
        User user = userMapper.selectByPrimaryKey(userId);
        String encodePwd = DigestUtils.md5Hex(pwd + slat);
        if (user == null) {
            return new LoginUser();
        }
        if (encodePwd.equals(user.getPwd())) {
            return loginUser;
        }else{
            return new LoginUser();
        }

    }

    @Override
    public LoginUser register(String email,String pwd) {

        String userId  = IDUtils.toUUID();
        LoginUser loginUser = new LoginUser();
        loginUser.setEmail(email);
        loginUser.setUserId(userId);
        loginUser.setAvatar("http://ww1.sinaimg.cn/large/006MXs25gy1fzhqc11ghvj305k05k748.jpg");
        loginUser.setNickName("鲁班七号");
        loginUser.setRegTime(new Date());
        loginUser.setSlogan("格言都没有写呢~~~");
        loginUser.setGender(new Byte("0"));

        User user = new User();
        user.setUserId(userId);
        user.setPwdLength(pwd.length());
        user.setPwd(DigestUtils.md5Hex(pwd+slat));
        user.setEncrypt("md5");
        user.setLastPwd(DigestUtils.md5Hex(pwd + slat));


        loginUserMapper.insert(loginUser);
        userMapper.insert(user);
        return loginUser;
    }

    @Override
    public Result update(LoginUser user) {

        Result result = new Result();

        LoginUser loginUser = loginUserMapper.selectByPrimaryKey(user.getUserId());
        if (loginUser == null) {
            result.setSuccess(false);
            result.setErrMsg("用户不存在！");
            return result;
        }

        int i = loginUserMapper.updateByPrimaryKey(user);
        if (i > 0) {
            result.setSuccess(true);
        }else{
            result.setSuccess(false);
            result.setErrMsg("更新用户信息失败！");
        }
        return result;

    }

    @Override
    public boolean updatePwd(String userId, String pwd) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (user == null) {
            return false;
        }
        user.setLastPwd(user.getPwd());
        user.setPwd(pwd);
        userMapper.updateByPrimaryKey(user);
        return true;
    }

    @Override
    public boolean forgetPwd(String userId) {

        return false;
    }

    @Override
    public LoginUser getUserWithId(String userId) {
        LoginUser loginUser = loginUserMapper.selectByPrimaryKey(userId);
        if (loginUser == null) {
            return new LoginUser();
        }else{
            return loginUser;
        }
    }

    @Override
    public LoginUser getUserWithMail(String email) {
        LoginUser loginUser = loginUserMapper.selectByEmail(email);
        if (loginUser == null) {
            return new LoginUser();
        }else{
            return loginUser;
        }
    }
}
